import axios from "axios"; //引入
import Vue from "vue";
import router from "@/router";
export let baseURL = process.env.VUE_APP_BASE_URL;

// 创建 vue 实例
let _this = new Vue();

let config = {
  baseURL: baseURL,
  timeout: 30000 //设置最大请求时间
};

const service = axios.create(config);

const err = (error) => {
  if (error.response) {
    console.log("------err-状态异常响应------", error.response);
    if (error.response.status >= 400) {
      _this.$toast.clear();
      _this.$toast.err("服务器错误，请稍后再试！");
    };
  };
  return Promise.reject(error);
};


// HTTPresponse拦截
service.interceptors.response.use((response) => {
  // console.log("------服务异常响应------", response);
  if (response.data.code !== 200) {
    switch (response.data.code) {
      case 403:
        _this.$toast.err("登录超时");
        router.currentRoute.path !== "login" &&
          router.replace({
            name: "Login"
          });
        break;
    }
  }
  return response.data;
}, err);

//封装post,get方法
const request = {
  get(url = "", params = {}, isShowMsg = true) {
    return new Promise((resolve, reject) => {
      service({
          url,
          params,
          headers: {
            Accept: "application/json"
          },
          method: "GET"
        })
        .then((res) => {
          if (res.status !== 1) {
            if (isShowMsg) {
              _this.$toast.err(res.msg ? res.msg : "服务器错误");
              return;
            }
          }
          resolve(res);
          return res;

        })
        .catch((error) => {
          _this.$toast.err("网络错误");
          reject(error);
        });
    });
  },
  post(url = "", params = {}, isShowMsg = true) {
    return new Promise((resolve, reject) => {
      service({
          url,
          data: params,
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json;charset=UTF-8",
            // 'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: "POST"
        })
        .then((res) => {
          if (res.status !== 1) {
            if (isShowMsg) {
              console.log("isShowMsg===========", isShowMsg);
              _this.$toast.err(res.msg ? res.msg : "服务器错误");
               return;
            }
          }
          resolve(res);
          return res;
        })
        .catch((error) => {
          _this.$toast.err("网络错误");
          reject(error);
        });
    });
  },
  formPost(url = "", params = {}, isShowMsg = true) {
    return new Promise((resolve, reject) => {
      service({
          url,
          data: params,
          headers: {
            Accept: "application/json",
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: "POST"
        })
        .then((res) => {
          if (res.status !== 1) {
            if (isShowMsg) {
              console.log("isShowMsg===========", isShowMsg);
              _this.$toast.err(res.msg ? res.msg : "服务器错误");
               return;
            }
          }
          resolve(res);
          return res;
        })
        .catch((error) => {
          _this.$toast.err("网络错误");
          reject(error);
        });
    });
  },
};


export default request;